home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / frasr182.zip / PARSER.C < prev    next >
C/C++ Source or Header  |  1993-07-13  |  56KB  |  2,251 lines

  1. /* Parser.c (C) 1990, Mark C. Peterson, CompuServe [70441,3353]
  2.    All rights reserved.
  3.  
  4.    Code may be used in any program provided the author is credited
  5.     either during program execution or in the documentation.  Source
  6.     code may be distributed only in combination with public domain or
  7.     shareware source code.  Source code may be modified provided the
  8.     copyright notice and this message is left unchanged and all
  9.     modifications are clearly documented.
  10.  
  11.     I would appreciate a copy of any work which incorporates this code,
  12.     however this is optional.
  13.  
  14.     Mark C. Peterson
  15.     405-C Queen St. Suite #181
  16.     Southington, CT 06489
  17.     (203) 276-9721
  18. */
  19.  
  20. /*    Chuck Ebbert (CompuServe [76306,1226] ) changed code marked 'CAE fp'    */
  21. /*   for fast 387 floating-point math.  See PARSERA.ASM and PARSERFP.C */
  22. /*   (13 Dec 1992.)  */
  23. /* */
  24. /*   Modified 12 July 1993 by CAE to fix crash when formula not found.  */
  25.  
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <float.h>                              /* TIW 04-22-91 */
  31. #include <time.h>
  32. #include "mpmath.h"
  33. #include "prototyp.h"
  34.  
  35. extern int CvtStk(void);    /* CAE fp */
  36. extern int Transparent3D;                         /* MCP 5-30-91 */
  37.  
  38. #ifdef WATCH_MP
  39. double x1, y1, x2, y2;
  40. #endif
  41.  
  42. MATH_TYPE MathType = D_MATH;
  43. /* moved _LCMPLX and union ARg to mpmath.h -6-20-90 TIW */
  44.  
  45. /* PB 910417 added MAX_OPS and MAX_ARGS defines */
  46. #define MAX_ARGS 100
  47. #define MAX_OPS 250
  48. struct PEND_OP {
  49.    void (far *f)(void);
  50.    int p;
  51. };
  52. /* CAE fp added MAX_STORES and LOADS */
  53. #define MAX_STORES 125  /* at most only half the ops can be stores */
  54. #define MAX_LOADS  200  /* and 80% can be loads */
  55.  
  56. /* PB 901103 made some of the following static for safety */
  57. static struct PEND_OP far *o;
  58.  
  59. static void parser_allocate(void);
  60.  
  61. union Arg *Arg1, *Arg2;
  62. /* PB 910417 removed unused "a" array */
  63.  
  64. /* CAE fp  made some of the following non-static for PARSERA.ASM */
  65. /* Some of these variables should be renamed for safety */
  66. union Arg s[20], far * far *Store, far * far *Load;    /* static CAE fp */
  67. int StoPtr, LodPtr, OpPtr;    /* static CAE fp */
  68.  
  69. struct fls { /* function, load, store pointers  CAE fp */
  70.    void (near *function)(void);
  71.    union Arg near *operand;
  72. };
  73. extern struct fls far *pfls; /* init in parserfp.c  CAE fp */
  74. void (far * far *f)(void) = (void(far * far *)(void))0;    /* static CAE fp */
  75.  
  76. unsigned vsp, LastOp;    /* CAE fp made non-static */
  77. static unsigned n, ErrPtr, posp, NextOp, InitN;
  78. static int paren, SyntaxErr, ExpectingArg;
  79. struct ConstArg far *v = (struct ConstArg far *)0;    /* was static CAE fp */
  80. int InitLodPtr, InitStoPtr, InitOpPtr, LastInitOp;    /* was static CAE fp */
  81. static int Delta16;
  82. double fgLimit;           /* TIW 05-04-91 */
  83. static double fg;
  84. static int ShiftBack;     /* TIW 06-18-90 */
  85. static int SetRandom;     /* MCP 11-21-91 */
  86. static int Randomized;
  87. static unsigned long RandNum;
  88.  
  89. extern int bitshift;
  90. extern int bitshiftless1;
  91. extern int symmetry;          /* symmetry flag for calcmand()  */
  92. extern double param[];
  93.  
  94. extern int debugflag;         /* BDT for debugging */
  95. extern char boxx[8192];       /* PB 4-9-91, good place for the formula string */
  96. extern int row, col, overflow, cpu, fpu;
  97. extern _CMPLX old, new;
  98. extern double far *dx0, far *dy0;
  99. extern long far *lx0, far *ly0;     /* BDT moved these to FAR */
  100.  
  101. #ifndef TESTING_MATH
  102. extern double far *dx1, far *dy1;
  103. extern long far *lx1, far *ly1;
  104. #define dShiftx dx1[row]
  105. #define dShifty dy1[col]
  106. #define lShiftx lx1[row]
  107. #define lShifty ly1[col]
  108. #else
  109. #define dShiftx 0.0
  110. #define dShifty 0.0
  111. #define lShiftx 0L
  112. #define lShifty 0L
  113. #endif
  114.  
  115. extern _LCMPLX lold, lnew;
  116. extern char FormName[];
  117.  
  118. extern VOIDFARPTR typespecific_workarea;
  119.  
  120. #define LastSqr v[4].a
  121.  
  122. static char far * far ErrStrings[] = {   /* TIW 03-31-91 added far */
  123.    "Should be an Argument",
  124.    "Should be an Operator",
  125.    "')' needs a matching '('",
  126.    "Need more ')'",
  127.    "Undefined Operator",
  128.    "Undefined Function",
  129.    "More than one ','",
  130.    "Table overflow"
  131. };
  132.  
  133. unsigned SkipWhiteSpace(char *Str) {
  134.    unsigned n, Done;
  135.  
  136.    for(Done = n = 0; !Done; n++) {
  137.       switch(Str[n]) {
  138.       case ' ':
  139.       case '\t':
  140.       case '\n':
  141.       case '\r':
  142.          break;
  143.       default:
  144.          Done = 1;
  145.       }
  146.    }
  147.    return(n - 1);
  148. }
  149.  
  150. /* Random number code, MCP 11-21-91 */
  151.  
  152. unsigned long NewRandNum(void)
  153. {
  154.    return(RandNum = ((RandNum << 15) + rand15()) ^ RandNum);
  155. }
  156.  
  157. void lRandom(void)
  158. {
  159.    v[7].a.l.x = NewRandNum() >> (32 - bitshift);
  160.    v[7].a.l.y = NewRandNum() >> (32 - bitshift);
  161. }
  162.  
  163. void dRandom(void)
  164. {
  165.    long x, y;
  166.  
  167.    /* Use the same algorithm as for fixed math so that they will generate
  168.       the same fractals when the srand() function is used. */
  169.    x = NewRandNum() >> (32 - bitshift);
  170.    y = NewRandNum() >> (32 - bitshift);
  171.    v[7].a.d.x = ((double)x / (1L << bitshift));
  172.    v[7].a.d.y = ((double)y / (1L << bitshift));
  173. }
  174.  
  175. #ifndef XFRACT
  176. void mRandom(void)
  177. {
  178.    long x, y;
  179.  
  180.    /* Use the same algorithm as for fixed math so that they will generate
  181.       the same fractals when the srand() function is used. */
  182.    x = NewRandNum() >> (32 - bitshift);
  183.    y = NewRandNum() >> (32 - bitshift);
  184.    v[7].a.m.x = *fg2MP(x, bitshift);
  185.    v[7].a.m.y = *fg2MP(y, bitshift);
  186. }
  187. #endif
  188.  
  189. void SetRandFnct(void)
  190. {
  191.    unsigned Seed;
  192.  
  193.    if(!SetRandom)
  194.       RandNum = Arg1->l.x ^ Arg1->l.y;
  195.  
  196.    Seed = (unsigned)RandNum ^ (unsigned)(RandNum >> 16);
  197.    srand(Seed);
  198.    SetRandom = 1;
  199.  
  200.    /* Clear out the seed */
  201.    NewRandNum();
  202.    NewRandNum();
  203.    NewRandNum();
  204. }
  205.  
  206. void RandomSeed(void)
  207. {
  208.    time_t ltime;
  209.  
  210.    /* Use the current time to randomize the random number sequence. */
  211.    time(<ime);
  212.    srand((unsigned int)ltime);
  213.  
  214.    NewRandNum();
  215.    NewRandNum();
  216.    NewRandNum();
  217.    Randomized = 1;
  218. }
  219.  
  220. #ifndef XFRACT
  221. void lStkSRand(void)
  222. {
  223.    SetRandFnct();
  224.    lRandom();
  225.    Arg1->l = v[7].a.l;
  226. }
  227. #endif
  228.  
  229. #ifndef XFRACT
  230. void mStkSRand(void)
  231. {
  232.    Arg1->l.x = Arg1->m.x.Mant ^ (long)Arg1->m.x.Exp;
  233.    Arg1->l.y = Arg1->m.y.Mant ^ (long)Arg1->m.y.Exp;
  234.    SetRandFnct();
  235.    mRandom();
  236.    Arg1->m = v[7].a.m;
  237. }
  238. #endif
  239.  
  240. void dStkSRand(void)
  241. {
  242.    Arg1->l.x = (long)(Arg1->d.x * (1L << bitshift));
  243.    Arg1->l.y = (long)(Arg1->d.y * (1L << bitshift));
  244.    SetRandFnct();
  245.    dRandom();
  246.    Arg1->d = v[7].a.d;
  247. }
  248.  
  249. void (*StkSRand)(void) = dStkSRand;
  250.  
  251. void dStkAbs(void) {
  252.    Arg1->d.x = fabs(Arg1->d.x);
  253.    Arg1->d.y = fabs(Arg1->d.y);
  254. }
  255.  
  256. #ifndef XFRACT
  257. void mStkAbs(void) {
  258.    if(Arg1->m.x.Exp < 0)
  259.       Arg1->m.x.Exp = -Arg1->m.x.Exp;
  260.    if(Arg1->m.y.Exp < 0)
  261.       Arg1->m.y.Exp = -Arg1->m.y.Exp;
  262. }
  263.  
  264. void lStkAbs(void) {
  265.    Arg1->l.x = labs(Arg1->l.x);
  266.    Arg1->l.y = labs(Arg1->l.y);
  267. }
  268. #endif
  269.  
  270. void (*StkAbs)(void) = dStkAbs;
  271.  
  272. void dStkSqr(void) {
  273.    LastSqr.d.x = Arg1->d.x * Arg1->d.x;
  274.    LastSqr.d.y = Arg1->d.y * Arg1->d.y;
  275.    Arg1->d.y = Arg1->d.x * Arg1->d.y * 2.0;
  276.    Arg1->d.x = LastSqr.d.x - LastSqr.d.y;
  277.    LastSqr.d.x += LastSqr.d.y;
  278.    LastSqr.d.y = 0;
  279. }
  280.  
  281. #ifndef XFRACT
  282. void mStkSqr(void) {
  283.    LastSqr.m.x = *MPmul(Arg1->m.x, Arg1->m.x);
  284.    LastSqr.m.y = *MPmul(Arg1->m.y, Arg1->m.y);
  285.    Arg1->m.y = *MPmul(Arg1->m.x, Arg1->m.y);
  286.    Arg1->m.y.Exp++;
  287.    Arg1->m.x = *MPsub(LastSqr.m.x, LastSqr.m.y);
  288.    LastSqr.m.x = *MPadd(LastSqr.m.x, LastSqr.m.y);
  289.    LastSqr.m.y.Mant = (long)(LastSqr.m.y.Exp = 0);
  290. }
  291.  
  292. void lStkSqr(void) {
  293.    LastSqr.l.x = multiply(Arg1->l.x, Arg1->l.x, bitshift);
  294.    LastSqr.l.y = multiply(Arg1->l.y, Arg1->l.y, bitshift);
  295.    Arg1->l.y = multiply(Arg1->l.x, Arg1->l.y, bitshift) << 1;
  296.    Arg1->l.x = LastSqr.l.x - LastSqr.l.y;
  297.    LastSqr.l.x += LastSqr.l.y;
  298.    LastSqr.l.y = 0L;
  299. }
  300. #endif
  301.  
  302. void (*StkSqr)(void) = dStkSqr;
  303.  
  304. void dStkAdd(void) {
  305.    Arg2->d.x += Arg1->d.x;
  306.    Arg2->d.y += Arg1->d.y;
  307.    Arg1--;
  308.    Arg2--;
  309. }
  310.  
  311. #ifndef XFRACT
  312. void mStkAdd(void) {
  313.    Arg2->m = MPCadd(Arg2->m, Arg1->m);
  314.    Arg1--;
  315.    Arg2--;
  316. }
  317.  
  318. void lStkAdd(void) {
  319.    Arg2->l.x += Arg1->l.x;
  320.    Arg2->l.y += Arg1->l.y;